package com.fanbuchi.app.rxjava;

import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        hellorxjava();
    }



    public void hellorxjava(){



        Observable<String> myObservable= Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("Hello, world!");
                subscriber.onCompleted();
            }
        });

        Subscriber<String> mySubscriber = new Subscriber<String>() {
            @Override
            public void onNext(String s) { System.out.println(s); }
            @Override
            public void onCompleted() { }
            @Override
            public void onError(Throwable e) { }
        };
        myObservable.subscribe(mySubscriber);

        Observable<String> myObservable2 = Observable.just("Hello, world! 2");
        Action1<String> mysub2 = new Action1<String>() {
            @Override
            public void call(String s) {
                System.out.println(s);
            }
        };

        myObservable2.subscribe(mysub2);


        Observable.just("Hello, world!")
                .map(new Func1<String, String>() {
                    @Override
                    public String call(String s) {
                        return s + " -3";
                    }
                })
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        System.out.println(s);
                    }
                });


        String[] urls = {"url1", "url2", "url3"};
        Observable.from(urls).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                System.out.println(s);
            }
        });



        /*myObservable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
        .subscribe(bitmap -> myImageView.setImageBitmap(bitmap));*/


     /*   AndroidObservable.bindActivity(this, retrofitService.getImage(url))
                .subscribeOn(Schedulers.io())
                .subscribe(bitmap -> myImageView.setImageBitmap(bitmap);*/
        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
       /* AndroidObservable.fromBroadcast(this, filter)
                .subscribe(intent -> handleConnectivityChange(intent));
        //最后说一下ViewObservable，它在Views的基础上增加了两个绑定：ViewObservable.clicks()和ViewObservable.text()。ViewObservable.clicks()的作用是获得View每次单击的事件，ViewObservable.text()则是用来获取TextView每次改变内容的事件。
        ViewObservable.clicks(mCardNameEditText, false)
                .subscribe(view -> handleClick(view));*/


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
